#include "Scene0.h"
#include <daisy\OgreGraphicsManager.h>
#include <daisy\EventSystem.h>
#include <OGRE\Ogre.h>
#include <OgreNewt.h>
#include <daisy\NewtonPhysicsManager.h>
#include <tinyxml\tinyxml.h>
#include "ModelConfig.h"
#include "Branch1EventDef.h"
#include "UtilsOgreDshow.h"
#include <boost/bind.hpp>
void defineTerrain(long x, long y) ;
Scene0::Scene0(const std::string &name):
Branch1Scene(name),
_initIsUpdate(false),
_initIsEnable(false)
{}
Scene0::~Scene0(){}
void Scene0::doEnableEx()
{
	enableUpdate();
	addChannel(DAISY::EventChannel::create().addSystemChannel<1>());

	Ogre::Viewport* vp = DAISY::OgreGraphicsManager::getInstance().getRenderWindow()->getViewport(0);
	Ogre::Camera* camera = DAISY::OgreGraphicsManager::getInstance().getSceneManager()->createCamera("MainCamera");
	_moviePlayer.reset(new OgreUtils::DirectShowManager(vp));
	DAISY::OgreGraphicsManager::getInstance().getRoot()->addFrameListener(_moviePlayer.get()); 
	_movieController = OgreUtils::DirectShowManager::getSingleton().createDirectshowControl("videotest","1kai.avi",vp->getActualHeight(),vp->getActualWidth());
	
	DAISY::OISInputManager::getInstance().addKeyListener(this);
	


}
void Scene0::doDisableEx()
{
	
	_movieController->StopMovie();


	_moviePlayer->DestroyAll();
	DAISY::OgreGraphicsManager::getInstance().getRoot()->removeFrameListener(_moviePlayer.get());
	DAISY::OISInputManager::getInstance().removeKeyListener(this);
	
	
}
void Scene0::doFrameEx()
{
	DAISY::OgreNewtManager::getInstance().getNewtWorld()->update(getInterval());
	if(!_movieController->IsPlaying())
	{
		Ogre::LogManager::getSingleton().logMessage("movie played up");
		Send_CHNSCENE("SceneTest");
	}
	

	
}
void Scene0::doExecuteEx(DAISY::Event* evt){}
void Scene0::Send_CHNSCENE(const std::string& sceneName)
{	
	disableUpdate();
	DAISY::Event* evt = DAISY::EventFactory::getInstance().createEvent();
	evt->setMsg(DAISY::BRANCH1Events::CHNSCENE);
	evt->setSender(shared_from_this());
	evt->setData<std::string>(sceneName);

	getEventManager()->broadcasting(evt);
	
}
bool Scene0::onKeyPressed(const KeyEvent & evt)
{
	using namespace DAISY;
	switch(evt.getKey())
	{
		case DAISY::KC_RETURN:
			Send_CHNSCENE("SceneTest");
			break;
	}
	return true;
}
bool Scene0::onKeyReleased(const KeyEvent & evt)
{
	return true;
}
void Scene0::initDateFromCallback(bool isEnable,bool isUpdate)
{
	_initIsEnable = isEnable;
	_initIsUpdate = isUpdate;
}
const std::string & Branch1Scene0Factory::getTypeName() const
{
	static const std::string typeName("Scene0");	
	return typeName;
}
Branch1Scene0Factory::pointer_type Branch1Scene0Factory::createInstance(const std::string& instanceName, parameter_type parameter)
{
	Scene0* scene0 = new Scene0(instanceName);
	scene0->initDateFromCallback(DAISY::VariantData<bool>::get((*parameter)["initIsEnable"]),
		DAISY::VariantData<bool>::get((*parameter)["initIsUpdate"]));
	
	Branch1::Branch1Scene* scene =  scene0;
		

	return pointer_type(scene);
}
